const app = getApp();
const Api = app.globalData.Api;
const wxApi = app.globalData.wxApi;
const util = app.globalData.util;
Page({
  data: {
    practices: [],
    subscribType: 2,
    loadMoreType: 0,
    conditions: [], //筛选列表
    popviewShow: false, //是否显示筛选
    selectedOptions: [],
    infoViewShow: false
  },
  onLoad(options) {

    this.init(options);
  },
  init(options) {
    let type = options.type || 2
    this.setData({
      subscribType: type
    });
    this.subscribCondition = null; //是否订阅选项
    this.allConditions = []; 
    this.mineConditions = [];
    this.allSelectedConditions = []; 
    this.mineSelectedConditions = [{
      id: 1,
      multiple: true,
      name: "是否已订阅",
      options: [{
        id: 1
      }]
    }];
    this.pageNo = 1;
    this.refresh();
    this.selectedOptions = [];
    this.practicesConditions();
  },
  refresh( conditions ) {
    this.pageNo = 1;
    this.setData({
      loadMoreType: 0
    })
    this.loadPractices(this.pageNo, conditions, (records) => {
      this.setData({
        practices: records,
        infoViewShow: !records.length
      })
    })
  },
  onReachBottom() {
    console.log('加载更多');
    this.loadMore();
  },
  loadMore() {
    let self = this;
    let practices = this.data.practices;
    this.pageNo++;
    console.log(this.pageNo);
    this.setData({
      loadMoreType: 1 // 显示加载更多
    })
    this.loadPractices(this.pageNo, null, data => {
      if (data.length) {
        practices = practices.concat(data);
        self.setData({
          loadMoreType: 1, // 显示加载更多
          practices: practices
        })
      } else {
        self.pageNo--; //没有更多则页码减一
        self.setData({
          loadMoreType: 2 // 显示无更多
        })
      }
    })
  },
  gotoPracticeDetail( e ) {
    let practice = e.currentTarget.dataset.practice;
    let params = {
      path: 'exercise',
      module: 'detail',
      type: practice.type,
      practiceId: practice.id
    }
    wxApi.navigateTo('../common/nonsupport/nonsupport', params)
  },
  getConditions() {
    let type = this.data.subscribType;
    const subscribOptionMap = {
      0: [{
        id: 2
      }], //为订阅
      1: [{
        id: 1
      }], //已订阅
      2: [{
        id: 1
      }, {
        id: 2
      }] //全部
    }
    return subscribOptionMap[type];
  },
  loadPractices(pageNo, conditions, callback) {
    if (!conditions) {
      conditions = [{
        id: 1,
        multiple: true,
        name: "是否已订阅",
        options: this.getConditions()
      }]
    }
    let params = {
      conditions: conditions,
      pageNo: pageNo,
      pageSize: 10
    }
    Api.practices(params, res => {
      callback(res.records);
      console.log(res);
    })
  },
  /**
   * 筛选确认
   */
  onSelecterSure( e ) {
    console.log( e );
    let type = this.data.subscribType;
    let all = e.detail.all;
    let selected = e.detail.selected;
    if ( type == 2 ) {
      this.allConditions = all;
      this.allSelectedConditions = selected
    }
    if ( type == 1 ) {
      this.mineConditions = all;
      this.mineSelectedConditions = selected;
    }
    this.setData({
      popviewShow: false,
      selectedOptions: selected
    });
    this.refresh( selected );
  },
  /**
   * 加载筛选条件
   */
  practicesConditions(callback) {
    Api.practicesConditions(conditions => {
      conditions = this.packConditions( conditions );
      this.allConditions = conditions;
      this.mineConditions = util.deepClone( conditions );
      this.mineConditions.shift();
      this.setData({
        conditions: conditions
      })
    })
  },
  /**
   * 全部练习
   */
  allPractice() {
    console.log( this.allConditions )
    this.setData({
      subscribType: 2,
      conditions: this.allConditions
    });
    this.refresh( this.allSelectedConditions );
  },
  /**
   * 我的练习
   */
  minePractice() {
    console.log( this.mineConditions );
    this.setData({
      subscribType: 1,
      conditions: this.mineConditions
    });
    // 对已选中选项进行筛选
    this.refresh( this.mineSelectedConditions );
  },
  /**
   * 打开筛选侧边栏
   */
  openSelecter() {
    // 如果是我的练习，则删除订阅选项
     
    wx.hideTabBar();
    // let popviewShow = this.data.popviewShow;
    this.setData({
      popviewShow: true
    })
  },
  onUpdateAttr(e) {
    
    setTimeout(() => {
      wx.showTabBar();
    }, 300);
    this.setData(e.detail);
  },
  /*
   * 包装筛选条件数组对象，利于后续选中逻辑
   */
  packConditions: function (data) {
    for (let i = 0, len_i = data.length; i < len_i; i++) {
      for (let j = 0, len_j = data[i].options.length; j < len_j; j++) {

        data[i].options[j].active = false;
        if (data[i].options[j].name.length > 6) {
          data[i].options[j].long = true;
        } else {
          data[i].options[j].long = false;
        }
      }
    }
    
    return data;
  }
})